package com.example.iot.queue.inMemory;

import com.example.iot.queue.QueueProducer;
import com.example.iot.queue.QueueCallback;
import com.example.iot.queue.TopicPartitionInfo;
import com.example.iot.queue.message.QueueMsg;
import lombok.Data;

@Data
public class InMemoryQueueProducer<T extends QueueMsg> implements QueueProducer<T> {

    private final InMemoryStorage storage = InMemoryStorage.getInstance();

    private final String defaultTopic;

    public InMemoryQueueProducer(String defaultTopic) {
        this.defaultTopic = defaultTopic;
    }

    @Override
    public void init() {

    }

    @Override
    public void send(TopicPartitionInfo tpi, T msg, QueueCallback callback) {
        boolean result = storage.put(tpi.getFullTopicName(), msg);
        if (result) {
            if (callback != null) {
                callback.onSuccess(null);
            }
        } else {
            if (callback != null) {
                callback.onFailure(new RuntimeException("Failure add msg to InMemoryQueue"));
            }
        }
    }

    @Override
    public void stop() {

    }
}
